/*
 * Copyright (C) 2022 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package cn.kcrx.autoweightapp.fragment

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import cn.kcrx.autoweightapp.core.BaseFragment
import cn.kcrx.autoweightapp.databinding.FragmengQuerySelectdateBinding
import cn.kcrx.autoweightapp.fragment.viewmodes.FragmentQuerySelectDateVM
import cn.kcrx.autoweightapp.utils.XToastUtils
import com.xuexiang.xaop.annotation.Permission
import com.xuexiang.xaop.annotation.SingleClick
import com.xuexiang.xaop.consts.PermissionConsts
import com.xuexiang.xpage.annotation.Page
import com.xuexiang.xui.widget.picker.widget.TimePickerView
import com.xuexiang.xui.widget.picker.widget.builder.TimePickerBuilder
import com.xuexiang.xutil.data.DateUtils
import mu.KotlinLogging
import java.util.*


@Page(name = "称重记录查询")
class FragmentQuerySelectDate :BaseFragment<FragmengQuerySelectdateBinding>(){
    private val logger=KotlinLogging.logger(this.javaClass.simpleName)
   private val viewmodel:FragmentQuerySelectDateVM by viewModels()
    override fun viewBindingInflate(
        inflater: LayoutInflater,
        container: ViewGroup
    ): FragmengQuerySelectdateBinding {
      return FragmengQuerySelectdateBinding.inflate(inflater,container,false)
    }

    override fun initViews() {
        viewmodel.fromDate.observe(viewLifecycleOwner){
            binding!!.tvFromdate.text= DateUtils.date2String(
                it,
                DateUtils.yyyyMMdd.get()
            )
        }
        viewmodel.toDate.observe(viewLifecycleOwner){
            binding!!.tvTodate.text= DateUtils.date2String(
                it,
                DateUtils.yyyyMMdd.get()
            )
        }
    }

    override fun initListeners() {
        super.initListeners()
         binding!!.btnQuery.setOnClickListener {
           val  params = Bundle()
             params.putLong("fromDate",viewmodel.fromDate.value!!.time)
             params.putLong("toDate",viewmodel.toDate.value!!.time)
            openPage(FramgengQuery::class.java,params)
         }
        binding!!.btnFromdate.setOnClickListener {
            selectFromDate(it,true)
        }
        binding!!.btnTodate.setOnClickListener {
            selectFromDate(it,false)
        }
    }

    /***
     * 日期选择框弹出
     * @view 点击的button
     * @fromTo 开始日期true 结束如期false
     */
    lateinit var formDatePicker: TimePickerView
    @SingleClick
    private fun selectFromDate(it: View?,fromTo:Boolean) {
         formDatePicker= TimePickerBuilder(
            context
        ) { date: Date, _->
            if(fromTo)  {
                viewmodel.setFromDate(date)
            } else{
                viewmodel.setToDate(date)
            }
            XToastUtils.toast(
                DateUtils.date2String(
                    date,
                    DateUtils.yyyyMMdd.get()
                )
            )
        }
            .setTimeSelectChangeListener { date: Date? ->
                Log.i(
                    "pvTime",
                    "onTimeSelectChanged"
                )
            }
            .setTitleText("日期选择")
            .build()
//            // 这样设置日月年显示
//            mDatePicker.getWheelTime().getView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
        formDatePicker.show()
    }
}